package com.example.renyang.happysell;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.renyang.happysell.Tools.msell_food;
import com.example.renyang.happysell.net.HttpUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import infClass.Seller;

public class fragment_menu extends Fragment{
    private ListView lv;
    private TextView textView;
    private Handler hander;
    private List<String> ll=new ArrayList<String>();
    private boolean flag=true;
    private  List<Seller> sellers=new ArrayList<Seller>();
    private Seller [] sellerarry;
    private Context contex;
    final JSONArray[] obj = {new JSONArray()};
    private Button searchbt;
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        View view= inflater.inflate(R.layout.fragment_menu, container, false);
        searchbt=(Button)view.findViewById(R.id.search_bar_but);
        textView=(TextView)view.findViewById(R.id.search_bar_but);
        lv=(ListView)view.findViewById(R.id.search_listview_text);
        new Thread(){
            public void run(){

                try {
                    init();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("zx","init()方法抛出异常");
                }
            }
        }.start();
        hander=new android.os.Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case 1:
                        add();
                        Log.i("zx", "***********w");
                        break;
                    default:break;
                }
            }
        };
        try{
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Seller sellerl = sellers.get(position);
                Intent intent=new Intent();
                intent.setClass(contex,msell_food.class);
                intent.putExtra("sname",sellerl.getSname());
         //       Log.i("zx",sellerl.getStorename().toString()+"##############");
                intent.putExtra("storename",sellerl.getStorename());
                startActivity(intent);

            }
        });}catch(Exception e){
            e.printStackTrace();
            Log.i("zx","初始化时候报错");
        }
        searchbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queryshopname();
            }
        });
        return view;
    }

    private void queryshopname() {
        new Thread(){
            public void run(){
                String name=textView.getText().toString();
                String url=HttpUtil.BASE_URL+""+name;

            }
        }.start();
    }

    private void queryfoodlist(final Seller sellerl) {
        new Thread(){
            public void run(){
                try {

                }catch (Exception e){
                    e.printStackTrace();
                    Log.i("zx","点击listview发送请求出错");
                }
            }
       }.start();
    }

    public void init() throws  Exception{
                try {

                    Log.i("zx", "start" + "》》》》》》》》》》》");
                    obj[0] =queryini();
                    JSONArray array = obj[0];
                    Log.i("zx","***********1");
                    for(int i=0;i<array.length();i++){
                        Log.i("zx","***********2");
                        Seller seller=new Seller();

                        Log.i("zx","***********3");
                        JSONObject jsonObject=new JSONObject(array.get(i).toString());
                        Log.i("zx","***********4");
                        seller.setStorename(jsonObject.get("storename").toString());
                        Log.i("zx", "***********5");
                        seller.setCity(jsonObject.get("city").toString());
                        Log.i("zx", "***********6");
                        seller.setSname(jsonObject.get("sname").toString());
                        seller.setCellnumber(jsonObject.get("cellnumber").toString());
                        sellers.add(seller);
                        Log.i("zx", "***********d");
                    }
                    Log.i("zx","***********7");
                    Message m=new Message();
                    m.what=1;
                    hander.sendMessage(m);
                }catch (Exception e){
                    e.printStackTrace();
                    Log.i("zx","访问出错");
                }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        contex = activity;
    }

    private JSONArray queryini() throws  Exception{
        String url = HttpUtil.BASE_URL + "getResfesh?time=0";
        Log.i("zx",url+"...");
        String s = HttpUtil.getRequest(url);
        Log.i("zx",s+""+"tttttttttttttttttttttttttttttttttttt");
        return new JSONArray(s);
    }
    public void add(){
            ArrayList<HashMap<String,Object>> listItem=new ArrayList<HashMap<String, Object>>();
        for(int i=0;i<sellers.size();i++){
            HashMap<String,Object> map=new HashMap<String, Object>();
            map.put("title", ":" + sellers.get(i).getStorename());
            map.put("title1","");
            map.put("title2",":"+sellers.get(i).getCity());
            listItem.add(map);
        }
        ;
        SimpleAdapter simpleAdapter=new SimpleAdapter(contex,listItem,R.layout.items,new String[]{"image","title","title1","title2"},
                new int[]{R.id.image,R.id.title,R.id.title1,R.id.title2});
        lv.setAdapter(simpleAdapter);
    }
}
